package com.johnny.spring.servlet;

import com.johnny.spring.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class HelloServlet {
    @RequestMapping("/testServlet01")
    public String testServlet01(HttpServletRequest req, HttpServletResponse resp) {
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        System.out.println("name: " + name + ", age: " + age);
        return "OK";
    }

    @RequestMapping("/testServlet02")
    public String testServlet02(String name, String age) {
        System.out.println("name: " + name + ", age: " + age);
        return "OK";
    }

    @RequestMapping("/testServlet03")
    public String testServlet03(User user) {
        System.out.println(user);
        return "OK";
    }

    @RequestMapping("/testServlet04")
    public String testServlet04(String[] hobby) {
        System.out.println(Arrays.toString(hobby));
        return "OK";
    }

    @RequestMapping("/testServlet05")
    public String testServlet05(@RequestParam List<String> hobby) {
        System.out.println(hobby.toString());
        return "OK";
    }

    @RequestMapping("/testServlet06")
    public String testServlet06(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime dateTime) {
        System.out.println(dateTime);
        return "OK";
    }

    @RequestMapping("/testServlet07")
    public String testServlet07(@RequestBody User user) {
        System.out.println(user);
        return "OK";
    }

    @RequestMapping("/testServlet08/{name}/{age}")
    public String testServlet07(@PathVariable String name, @PathVariable String age) {
        System.out.println(name + ", " + age);
        return "OK";
    }
}
